
# 工作流查询请求类
class workflow_query_request:
    def __init__(self, page_num=1, page_size=10, query_prefix=""):
        """
        初始化工作流查询请求
        :param page_num: 页码
        :param page_size: 每页大小
        :param query_prefix: 查询前缀
        """
        self.page_num = page_num
        self.page_size = page_size
        self.query_prefix = query_prefix

# 工作流查询响应类
class workflow_query_response:
    def __init__(self, flows=None, total=0, page_num=1, page_size=10):
        """
        初始化工作流查询响应
        :param flows: 工作流列表
        :param total: 总数
        :param page_num: 页码
        :param page_size: 每页大小
        """
        self.flows = flows if flows else []
        self.total = total
        self.page_num = page_num
        self.page_size = page_size

# 工作流详情响应类
class workflow_get_response:
    def __init__(self, workflow_id="", name="", definition="", description="", role="", 
                 region="", execution_mode="", execution_timeout=0, created_time=0, updated_time=0):
        """
        初始化工作流详情响应
        """
        self.workflow_id = workflow_id
        self.name = name
        self.definition = definition
        self.description = description
        self.role = role
        self.region = region
        self.execution_mode = execution_mode
        self.execution_timeout = execution_timeout
        self.created_time = created_time
        self.updated_time = updated_time

# OpenAPI响应元数据类
class openapi_response_meta:
    def __init__(self, status_code=0, error="", message="", return_obj=None):
        """
        初始化OpenAPI响应元数据
        :param status_code: 状态码
        :param error: 错误信息
        :param message: 消息
        :param return_obj: 返回对象
        """
        self.status_code = status_code
        self.error = error
        self.message = message
        self.return_obj = return_obj

# 工作流服务类
class workflow_service:
    def __init__(self, client):
        """
        初始化工作流服务
        :param client: 客户端实例
        """
        self.client = client
        self.endpoint = client.config.endpoint

    def query_workflow(self, region, request):
        """
        查询工作流
        :param region: 区域ID
        :param request: 工作流查询请求
        :return: 响应结果
        """
        if not region:
            raise ValueError('region is required')
        
        # 构建URL参数
        params = {
            'queryPrefix': request.query_prefix,
            'pageNum': str(request.page_num),
            'pageSize': str(request.page_size)
        }
        
        # 构建请求头
        headers = {
            'Content-Type': 'application/json',
            'regionId': region
        }
        
        # 构建请求配置
        url = "openapi/v1/workflow/query"
        config = {
            'method': 'GET',
            'url': url,
            'headers': headers,
            'params': params
        }
        
        # 发送请求
        res = self.client.do_request(config)
        if isinstance(res, ValueError):
            print(f'query workflow err: {res}')
            return None, res
        
        # 解析响应
        try:
            import json
            response_data = json.loads(res)
            response = openapi_response_meta()
            response.status_code = response_data.get('statusCode', 0)
            response.error = response_data.get('error', '')
            response.message = response_data.get('message', '')
            
            # 解析返回对象
            return_obj_data = response_data.get('returnObj', {})
            return_obj = workflow_query_response()
            return_obj.total = return_obj_data.get('total', 0)
            return_obj.page_num = return_obj_data.get('pageNum', 1)
            return_obj.page_size = return_obj_data.get('pageSize', 10)
            
            # 解析工作流列表
            flows_data = return_obj_data.get('flows', [])
            flows = []
            for flow_data in flows_data:
                flow = workflow_get_response(
                    workflow_id=flow_data.get('workflowId', ''),
                    name=flow_data.get('name', ''),
                    definition=flow_data.get('definition', ''),
                    description=flow_data.get('description', ''),
                    role=flow_data.get('role', ''),
                    region=flow_data.get('region', ''),
                    execution_mode=flow_data.get('executionMode', ''),
                    execution_timeout=flow_data.get('executionTimeout', 0),
                    created_time=flow_data.get('createdTime', 0),
                    updated_time=flow_data.get('updatedTime', 0)
                )
                flows.append(flow)
            
            return_obj.flows = flows
            response.return_obj = return_obj
            
            return response, None
        except Exception as e:
            print(f'parse response err: {e}')
            return None, ValueError(f'parse response err: {e}')